package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.Attribute;
import com.example.demo.entity.Category;
import com.example.demo.model.Result;
import com.example.demo.service.AttributeService;
import com.example.demo.service.impl.AttributeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性表 前端控制器
 * </p>
 *
 * @author hpp
 * @since 2021-09-14
 */
@RestController
//@RequestMapping("/attribute")
public class AttributeController {
    @Autowired
    AttributeService attributeService;
    /*参数列表*/
    @GetMapping("/categories/{id}/attributes")
    public Result<List<Attribute>> getAttributesList(@PathVariable Integer id,
                                                     @RequestParam("sel") String sel){
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cat_id",id).eq("attr_sel",sel);
        List<Attribute> list = attributeService.list(queryWrapper);
        return new Result<>(200,"获取成功",list);
    }
    /*添加动态参数或者静态属性*/
    @PostMapping("categories/{id}/attributes")
    public Result<Attribute> addAttributes(@PathVariable Integer id,
                                           @RequestParam("attr_name") String attr_name,
                                           @RequestParam("attr_sel") String attr_sel,
                                           @RequestParam(value = "attr_vals",required = false,defaultValue = "null") String attr_vals){
        Attribute attribute = new Attribute();
        attribute.setCatId(id);
        attribute.setAttrName(attr_name);
        attribute.setAttrSel(attr_sel);
        if(attr_vals.equals("null")){
            attribute.setAttrVals(attr_vals);
        }
        boolean save = attributeService.save(attribute);
        Attribute byId = attributeService.getById(attribute.getAttrId());
        if(save){
            return new Result<>(201,"创建成功",byId);
        }
        return new Result<>(0,"创建失败",null);
    }
    /*删除参数*/
    @DeleteMapping("categories/{id}/attributes/{attrid}")
    public Result<Attribute> deleteAttributesById(@PathVariable Integer id,
                                                  @PathVariable Integer attrid){
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cat_id",id).eq("attr_id",attrid);
        boolean remove = attributeService.remove(queryWrapper);
        if(remove){
            return new Result<>(200,"删除成功",null);
        }
        return new Result<>(0,"删除失败",null);
    }
    /*根据 ID 查询参数*/
    @GetMapping("categories/{id}/attributes/{attrid}")
    public Result<Attribute> getAttributesById(@PathVariable Integer id,
                                               @PathVariable Integer attrid){
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cat_id",id).eq("attr_id",attrid);
        Attribute one = attributeService.getOne(queryWrapper);
        return new Result<>(200,"获取成功",one);
    }
    /*编辑提交参数*/
    @PutMapping("categories/{id}/attributes/{attrid}")
    public Result<Attribute> modifyAttributesById(@PathVariable Integer id,
                                                  @PathVariable Integer attrid,
                                                  @RequestParam("attr_name") String attr_name,
                                                  @RequestParam("attr_sel") String attr_sel,
                                                  @RequestParam(value = "attr_vals",required = false,defaultValue = "") String attr_vals){
        UpdateWrapper<Attribute> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cat_id",id).eq("attr_id",attrid)
                .set("attr_name", attr_name)
                .set("attr_sel",attr_sel)
                .set("attr_vals",attr_vals);
        boolean update = attributeService.update(updateWrapper);
        Attribute byId = attributeService.getById(attrid);
        if(update){
            return new Result<>(200,"更新成功",byId);
        }
        return new Result<>(0,"更新失败",null);
    }
    /**/
}

